package tq.com.tqteacher.mine.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import tq.com.tqcom.vh.ComSpacerViewHolder;
import tq.com.tqcom.vh.ComViewHolder;
import tq.com.tqteacher.R;

public class TQMineAdapter extends RecyclerView.Adapter {
    private MineData[] mData = new MineData[]{
            MineData.instance(R.drawable.icon_bjxx, "班级信息", 0),
            MineData.instance(R.drawable.icon_nrjj, "内容简介", 1),
            MineData.instance(R.drawable.icon_bkxx, "班卡信息", 2),
            MineData.instance(R.drawable.icon_bjgk, "班级概况", 3),
            new MineData(15),

            MineData.instance(R.drawable.icon_grsz, "个人设置", 4),
            MineData.instance(R.drawable.icon_lsjj, "老师简介", 5),
            MineData.instance(R.drawable.icon_bkjh, "班卡激活人数", 6),
            MineData.instance(R.drawable.icon_xsbk, "线上班卡购买人数", 7),
            new MineData(30)
    };

    private Context mContext;

    public TQMineAdapter(Context pContext){
        mContext = pContext;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder pViewHolder, int pI) {

        if(pViewHolder instanceof ItemViewHold){
            MineData lData = mData[pI];
            MineData next = mData[pI + 1];
            ((ItemViewHold)pViewHolder).bindData(lData, next.isSpcer);
        }else if(pViewHolder instanceof ComSpacerViewHolder){
            MineData lData = mData[pI];
            ((ComSpacerViewHolder)pViewHolder).bindHeight(lData.spcerHight);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup pViewGroup, int pI) {
        if(pI == 0){
            View lView = LayoutInflater.from(mContext).inflate(R.layout.vh_mine_item_layout, pViewGroup, false);
            return new ItemViewHold(lView);
        }else if(pI == 1){
            MineData lData = mData[pI];
            return ComSpacerViewHolder.instance(lData.spcerHight, pViewGroup, mContext);
        }else{
            return ComViewHolder.instance(mContext, pViewGroup, R.layout.vh_quite_layout)
                    .onClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(mIClickListener != null){
                                mIClickListener.quite();
                            }
                        }
                    });
        }
    }

    @Override
    public int getItemCount() {
        return mData.length + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if(position == mData.length){
            return 2;
        }
        MineData lData = mData[position];
        if(lData.isSpcer){
            return 1;
        }
        return 0;
    }

    static class MineData{
        private int resid;
        private String title;
        private int key;

        boolean isSpcer;
        private int spcerHight = 0;

        public static MineData instance(int resId, String title, int pKey){
            MineData lData = new MineData();
            lData.resid = resId;
            lData.title = title;
            lData.key = pKey;
            lData.isSpcer = false;
            return lData;
        }

        MineData(){}
        MineData(int h){
            isSpcer = true;
            spcerHight = h;
        }
    }


    class ItemViewHold extends RecyclerView.ViewHolder{
        private MineData mMineData;

        private ImageView mImageView;
        private TextView mTextView;
        private TextView mLine;

        public ItemViewHold(View pView){
            super(pView);

            pView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mMineData != null && mIClickListener != null){
                        mIClickListener.clickItem(mMineData.key);
                    }
                }
            });

            mImageView = pView.findViewById(R.id.icon);
            mTextView = pView.findViewById(R.id.title);
            mLine = pView.findViewById(R.id.line);
        }


        public void bindData(MineData pMineData, boolean h){
            mMineData = pMineData;
            if(mMineData == null || mMineData.isSpcer){
                return;
            }

            mImageView.setImageResource(pMineData.resid);
            mTextView.setText(pMineData.title);
            mLine.setVisibility(h ? View.INVISIBLE : View.VISIBLE);
        }


    }


    public interface IClickListener{
        void clickItem(int key);
        void quite();
    }

    private IClickListener mIClickListener;
    public void setIClickListener(IClickListener pIClickListener){
        mIClickListener = pIClickListener;
    }
}
